package cn.tedu._07springaop.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Component
public class LogAspect {
	
	@Pointcut("execution(public int cn.tedu._07springaop.aop.CalculatorImpl.*(..))")
	public void pointcut(){
	
	}
	@Before(value = "pointcut()")
	public void beforeMethod(JoinPoint joinPoint) {
		//1.获取目标方法名
		String name = joinPoint.getSignature().getName();
		//2.获取目标方法参数
		String args = Arrays.toString(joinPoint.getArgs());
		System.out.println("[前置通知]" + name + " 方法开始了,参数为:" + args);
	}
		@After(value = "pointcut()")
		public void afterMethod(JoinPoint joinPoint){
			//1.获取目标方法名
			String name = joinPoint.getSignature().getName();
			System.out.println("[后置通知]" + name + " 方法结束了");
		}
		@AfterReturning(value = "pointcut()", returning = "r")
		public void afterReturningMethod(JoinPoint joinPoint, Object r){
			String name = joinPoint.getSignature().getName();
			System.out.println("[返回通知]" + name + " 方法正常返回,结果为:" + r);
	}
	
	@AfterThrowing(value = "pointcut()", throwing = "ex")
		public void afterThrowingMethod(JoinPoint joinPoint, Throwable ex){
		String name = joinPoint.getSignature().getName();
		System.out.println("[异常通知]"+ name +"方法抛出了异常，提示消息为："+ex.getMessage());
	}
}
